package com.osce.face.controller;

import com.doudou.arc.ArcFaceEngine;
import com.doudou.core.container.ListUtil;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.interfaces.face.IFaceListService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author zzxjs
 * @since 2023/11/22
 */
@RestController
@RequestMapping("/face/test")
public class FaceTestController extends BaseController {

    @Resource
    private ArcFaceEngine arcFaceEngine;
    @Resource
    private IFaceListService faceListService;

    @PostMapping("/compare-file")
    public AjaxResult compareFile(@RequestParam("file") MultipartFile image) throws IOException {
        return success(faceListService.faceCompareFileBytes(image.getBytes()));
    }

    @PostMapping("/compare-base64")
    public AjaxResult compareBase64(@RequestBody String base64) {
        return success(faceListService.faceCompareBase64(base64));
    }

    @PostMapping("/compare-faceFeature")
    public AjaxResult compareFaceFeature(@RequestBody List<String> faceFeatures) {
        if (ListUtil.isEmpty(faceFeatures)) {
            return error("特征值不可为空");
        }
        return success(faceListService.faceCompare(faceFeatures.get(0)));
    }

    @PostMapping("/extract-file")
    public AjaxResult extract(@RequestParam("file") MultipartFile image) throws IOException {
        return success(arcFaceEngine.featureByteToStr(arcFaceEngine.extract(image.getBytes())));
    }

    @PostMapping("/extract-base64")
    public AjaxResult extract(@RequestBody String base64) throws IOException {
        return success(arcFaceEngine.featureByteToStr(arcFaceEngine.extract(base64)));
    }

}
